package com.lagou.client;

import com.lagou.encoder.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @author Mrwg
 * @date 2020/5/23
 * @description
 */
public class UserClientHander extends ChannelInboundHandlerAdapter implements Callable {
    private ChannelHandlerContext context;
    private String result;
    private RpcRequest para;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 􀶭􀚩􀹐􀛓􁒒􀷄􀴝􀒅􀠏􁯯􁒵􀮇􁕚􁑕
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        result = msg.toString();
        notify();
    }

    /**
     * 􀙟􀚊􀷄􀴝􀒅􀭏􀦤􁒵􀮇􀠏􁯯
     */
    @Override
    public synchronized Object call() throws InterruptedException {
        context.writeAndFlush(para);
        wait();
        return result;
    }

    void setPara(RpcRequest para) {
        this.para = para;
    }
}
